package com.dispatcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import cn.jpush.android.api.InstrumentedActivity;

public class MainActivity extends InstrumentedActivity {
	GlobalVar gvar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gift);
		gvar = (GlobalVar) getApplication();
		if(isRegister()) {
			startActivity(new Intent(this, HomeTabActivity.class));
		} else {
			startActivity(new Intent(this, RegisterActivity.class));
		}
		finish();
	}
	
	private boolean isRegister() {
		boolean ret = false;
		String presentId = getPresentId();
		if(presentId != null && !TextUtils.isEmpty(presentId)) {
			ret = true;
			gvar.initDBCtrl(presentId);
		}
		return ret;
	}

	// 从文件中读
	private String getPresentId() {
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)) {
			return null;
		}
		String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
		//先判断目录是否存在
		File dir = new File(sdcard + "/dayinxiang");
		if(!dir.exists()) {
			return null;
		}
		//判断文件是否存在
		File file = new File(sdcard + "/dayinxiang" + "/" + "presentid.txt");
		BufferedReader bReader;
		String line = null;
		if(file.exists()) {
			try {
				bReader = new BufferedReader(new FileReader(file));
				line = bReader.readLine();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return line;
	}
}
